define(["bbcare"], function ($bb) {
    var event = {}, vm = null, URL = $bb.baseUrl('followup')


    event.init = function () {
        if (vm) vm = null
        vm = createVue()
    }

    function createVue() {
        return new Vue({
            el: '#root',
            data: {
                table: {
                    data: [],
                    loading: false,
                    total: 0,
                    current: 1,
                    size: 10,
                },
                loading: false,
                search: {
                    name: '',
                    phone: '',
                    No: '',
                    BedNo: '',
                    roomNo: '',
                    wardNo: '',
                    loading: false
                },
            },
            created: function () {
                this.getTableList()
            },
            methods: {
                handleSearch: function () {
                    this.search.loading = true
                    this.getTableList()
                },
                /**
                 * 提交
                 */
                handleSubmit: function () {
                    var data = this.getSubmitData()
                    if (data.length === 0) return this.$message.error('请至少选择一项进行提交')
                    this.sendData(data)
                },
                handleSelectionChange: function (val) {
                    this.table.data.forEach(function (item) {
                        item.checked = false
                        val.forEach(function (row) {
                            if (item.id == row.id) item.checked = true
                        })
                    })
                },
                /**
                 * 获取提交的数据
                 */
                getSubmitData: function () {
                    var result = []
                    this.table.data.forEach(function (item) {
                        if (item.checked) result.push(item.id)
                    })
                    return result
                },
                sendData: function (data) {
                    var that = this
                    this.loading = true
                    $bb.ajax({
                        url: URL + 'followup/milkPreparationRecord/confirmSend',
                        data: JSON.stringify({
                            ids: data
                        }),
                        success: function (res) {
                            if (res.success) {
                                that.$message.success('提交成功')
                                that.getTableList()
                            } else {
                                that.$message.error(res.msg)
                            }
                            that.loading = false
                        }
                    })
                },
                /**
                 * 改变页面大小
                 */
                handleChangePage: function (num) {
                    this.table.current = num;
                    this.getTableList()
                },
                /**
                 * 改变当前页大小
                 * @param {*} num 
                 */
                handleSizeChange: function (num) {
                    this.table.size = num;
                    this.getTableList()
                },
                handleClickRow: function (row) {
                    this.$refs.data.toggleRowSelection(row);
                },
                /**
                 * 获取列表数据
                 */
                getTableList: function () {
                    this.table.loading = true
                    var that = this
                    var params = {
                        currentPage: this.table.current,
                        pageSize: this.table.size,
                        userName: this.search.name,
                        userId: '',
                        phone: this.search.phone,
                        status: '',
                        inPatientNo: this.search.No,
                        hospitalBedNo: this.search.BedNo,
                        hospitalRoomNo: this.search.roomNo,
                        hospitalWard: this.search.wardNo,
                        sendTime: '',
                        type: 1
                    }
                    $bb.ajax({
                        url: URL + 'followup/milkPreparationRecord/listPage',
                        data: JSON.stringify(params),
                        success: function (res) {
                            if (res.success) {
                                res.data.data.forEach(function (item, index) {
                                    item.No = (that.table.current - 1) * that.table.size + index + 1
                                    item.checked = false
                                })
                                that.table.data = res.data.data
                                that.table.total = res.data.count
                            } else {
                                that.$message.error('请求列表数据出错')
                            }
                            that.table.loading = false
                            that.search.loading = false
                        }
                    })
                },
                 /**
                 * 删除
                 */

                  handleDelete(index, row) {
                    console.log(row)
                    let that= this
                    this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                      }).then(() => {
                       
                        $bb.ajax({
                            url: URL + `followup/milkPreparationRecord/delete/${row.id}`,
                            success: function (res) {
                                if (res.success) {
                                    that.$message({
                                        type: 'success',
                                        message: '删除成功!'
                                      });
                                      that.getTableList()
                                } 
                              
                            }
                        })
                       
                      }).catch(() => {
                        this.$message({
                          type: 'info',
                          message: '已取消删除'
                        });          
                      });
                }
            }
        })
    }

    return event
})